package com.example.demo.test;

import com.example.demo.bean.TestType;
import com.example.demo.bean.entity.TFlow;
import com.example.demo.bean.entity.TTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class ProcessContext {
    @Autowired
    public Map<String, Process> strategyMap = new ConcurrentHashMap<>(3);

    /**
     * 运行测试
     *
     * @param info
     */
    public void run(TTest info) {
        TFlow testProcessById = getTestProcessById(info.getFid());
        strategyMap.get(testProcessById.getType()).run(info);
    }

    public List<TFlow> getTestProcessByType(String type) {
        List<TFlow> pageFlowList = new ArrayList<>();
        strategyMap.forEach((k, v) -> {
            String name = v.getClass().getInterfaces()[0].getName();
            if (name.contains(type)) {
                pageFlowList.add(v.getFlowChart());
            }
        });
        return pageFlowList;
    }

    public TFlow getTestProcessById(Integer id) {
        if(id==null){
            return null;
        }
        Set<Map.Entry<String, Process>> entries = strategyMap.entrySet();
        for (Map.Entry<String, Process> next : entries) {
            TFlow flowChart = next.getValue().getFlowChart();
            Integer id1 = flowChart.getId();
            if (id.equals(id1)) {
                return flowChart;
            }
        }
        return null;
    }

    public List<TestType> getTestType() {
        List<TestType> testTypes = new ArrayList<>();
        strategyMap.forEach((k, v) -> {
            String name = v.getClass().getInterfaces()[0].getName();
            String supertype = name.substring(name.lastIndexOf(".") + 1);
            testTypes.add(new TestType(k, supertype));
        });
        return testTypes;
    }
}
